﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Common;

namespace Things.Web.Components
{
    public partial class TopStatus : System.Web.UI.UserControl
    {
        protected Things.Model.Buyer buyer
        {
            get
            {
                Things.Model.Buyer temp = null;

                if (Session[ConstantString.SessionCurrentBuyerAccount] != null)
                {
                    temp = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
                }

                return temp;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckVisibility();
            }
        }

        protected void CheckVisibility()
        {
            if (buyer != null)
            {
                lbtLogin.Visible = false;
                lbtRegister.Visible = false;

                lblUserName.Text = "您好，" + GetUserName();
                lblUserName.Visible = true;
                lbtLogout.Visible = true;
            }
            else
            {
                lbtLogin.Visible = true;
                lbtRegister.Visible = true;

                lblUserName.Text = "";
                lblUserName.Visible = false;
                lbtLogout.Visible = false;
            }
        }

        protected void lbtLogout_Click(object sender, EventArgs e)
        {
            Session[ConstantString.SessionCurrentBuyerAccount] = null;
            Response.Redirect("~/login.aspx");
        }

        protected string GetUserName()
        {
            string result = string.Empty;

            if (buyer != null)
            {
                Things.BLL.BuyerInfo biBLL = new BLL.BuyerInfo();
                Things.Model.BuyerInfo biModel = biBLL.GetModel(buyer.Account);
                if (biModel != null)
                {
                    if (string.IsNullOrEmpty(biModel.PetName) == false)
                    {
                        return StringRemove(biModel.PetName, 11);
                    }
                    if (string.IsNullOrEmpty(biModel.RealName) == false)
                    {
                        return StringRemove(biModel.RealName, 11);
                    }

                }                   
            }

            // 如果没有详细个人信息，就去account作为用户名

            string temp = buyer.Account.Split(new string[] { "@" }, StringSplitOptions.None)[0];
            // 避免用户名过长
            result = StringRemove(temp, 11);

            return result;
        }

        protected string StringRemove(string temp, int length)
        {
            string result = string.Empty;
            // 避免用户名过长
            if (temp.Length > length)
            {
                result = temp.Remove(0, length);
            }
            else
            {
                result = temp;
            }
            return result;
        }
    }
}